package com.ra.landlord;

import java.util.Iterator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;

import com.ra.system.Version;

/**
 * 某房间的收费规则
 * 
 * @author Jerry
 *
 */
@Entity
@Table(name = "ra_feerule")
@Where(clause = "data_status = 1")
public class FeeRule implements Version, LandlordOwner{
	
	private Integer id;
	
	private Room room;
	
	private ShareUnit shareUnit;
	
	private FeeRuleCfg feeRuleCfg;
	
	private List<Meter> meters;
	
	private LandlordOwnerUser landlordUser;
	
	private Integer version;
	
	private Integer status;

	@Id
	@GeneratedValue
	@Column(name = "relationId")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@ManyToOne(targetEntity=Room.class, fetch=FetchType.LAZY)
	@JoinColumn(name = "MasterId")
	@Index(name="idx_ra_feerule_MasterId")
	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}
	
	@ManyToOne(targetEntity=ShareUnit.class, fetch=FetchType.LAZY)
	@JoinColumn(name = "Master2Id")
	public ShareUnit getShareUnit() {
		return shareUnit;
	}

	public void setShareUnit(ShareUnit shareUnit) {
		this.shareUnit = shareUnit;
	}

	@ManyToOne(targetEntity=FeeRuleCfg.class, fetch=FetchType.LAZY)		// cascade=CascadeType.MERGE
	@JoinColumn(name = "SecondaryId")
	@Index(name="idx_ra_feerule_SecondaryId")
	@NotFound(action=NotFoundAction.IGNORE)
	public FeeRuleCfg getFeeRuleCfg() {
		return feeRuleCfg;
	}

	public void setFeeRuleCfg(FeeRuleCfg feeRuleCfg) {
		this.feeRuleCfg = feeRuleCfg;
	}

	@OneToMany(targetEntity=Meter.class, mappedBy="feeRule",fetch=FetchType.LAZY)		// cascade=CascadeType.MERGE
	@OrderBy("checkTime desc")
	@Where(clause = "data_status=1")
	@BatchSize(size=2)
	public List<Meter> getMeters() {
		return meters;
	}

	public void setMeters(List<Meter> meters) {
		this.meters = meters;
	}
	
	@Transient
	public Meter getCurrentMeter() {
		return getMeter(0);
	}
	
	public Meter getMeter(int index) {
		Iterator<Meter> iter = getMeters().iterator();
		Meter meter = null;
		while (index >= 0 && iter.hasNext()) {
			index--;
			meter = iter.next();
		}
		return meter;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "createuserid")
	public LandlordOwnerUser getLandlordUser() {
		return landlordUser;
	}

	public void setLandlordUser(LandlordOwnerUser landlordUser) {
		this.landlordUser = landlordUser;
	}
	
	@Column(name = "version_id")
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	@Column(name = "data_status")
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

}